package com.wsz.dao;

import com.wsz.bean.User;
import com.wsz.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoIpml implements UserDao {

    private static final String SQL_USER_LOGIN = "SELECT type from user WHERE uname=? AND upass=?";
    private static final String SQL_USER_INSERT = "INSERT INTO `user`(uname,upass,type) VALUES(?,?,2)";
    private static final String SQL_USER_DELETE = "DELETE from `user` WHERE uname=?";
    private static final String SQL_USER_UPDATE = "UPDATE `user` set upass=? WHERE uname=?";
    private static final String SQL_USER_SELECT = "SELECT id,uname,upass,type from `user` WHERE uname=?";
    @Override
    public int login(User user) {
        //连接数据库，创建连接对象conn
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtils.getConnection();
            //创建预编译环境
            preparedStatement = connection.prepareStatement("SELECT type from `user` WHERE uname= ? AND upass= ?");
            preparedStatement.setString(1, user.getUname());
            preparedStatement.setString(2, user.getUpass());
            //执行语句
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                //用结果集查询当前用户权限
                int type = resultSet.getInt("type");
                return type;
            }
        } catch (SQLException throwables) {
            // TODO Auto-generated catch block
            throwables.printStackTrace();
        }
         finally {
            JDBCUtils.release(connection, preparedStatement, resultSet);
        }
        return -1;
    }


    @Override
    public boolean insert(User user) {
        // 创建连接
        Connection connection = JDBCUtils.getConnection();
        // 创建预编译环境
        PreparedStatement preparedStatement = null;
        try {
            // 创建预编译环境
            preparedStatement = connection.prepareStatement(SQL_USER_INSERT);
          // 设置SQL语句中的参数
            preparedStatement.setString(1, user.getUname());
            preparedStatement.setString(2, user.getUpass());
            int line = preparedStatement.executeUpdate();
            return line>0?true:false;
        } catch (SQLException throwables) {
            // TODO Auto-generated catch block
            throwables.printStackTrace();
        }finally {
            JDBCUtils.release(connection,preparedStatement,null);
        }
        // INSERT INTO ‘user’ VALUES(id,"土狗","tg",2)
        return false;
    }

    @Override
    public boolean delete(String uname) {

        // 创建连接
        Connection connection = JDBCUtils.getConnection();
        // 创建预编译环境
        PreparedStatement preparedStatement = null;
        try {
            // 创建预编译环境
            preparedStatement = connection.prepareStatement(SQL_USER_DELETE);
            // 设置SQL语句中的参数
            preparedStatement.setString(1, uname);
            int line = preparedStatement.executeUpdate();
            return line>0?true:false;
        } catch (SQLException throwables) {
            // TODO Auto-generated catch block
            throwables.printStackTrace();
        }finally {
            JDBCUtils.release(connection,preparedStatement,null);
        }
        return false;
    }

    @Override
    public boolean update(User user) {

            // 创建连接
            Connection connection = JDBCUtils.getConnection();
            // 创建预编译环境
            PreparedStatement preparedStatement = null;
            try {
                // 创建预编译环境
                preparedStatement = connection.prepareStatement(SQL_USER_UPDATE);
                // 设置SQL语句中的参数
                preparedStatement.setString(1, user.getUpass());
                preparedStatement.setString(2, user.getUname());
                int line = preparedStatement.executeUpdate();
                return line>0?true:false;
            } catch (SQLException throwables) {
                // TODO Auto-generated catch block
                throwables.printStackTrace();
            }finally {
                JDBCUtils.release(connection,preparedStatement,null);
            }
        return false;
    }

    @Override
    public User select(String uname) {
        // 创建连接
        Connection connection = JDBCUtils.getConnection();
        // 创建预编译环境
        PreparedStatement preparedStatement = null;
        try {
            // 创建预编译环境
            preparedStatement = connection.prepareStatement(SQL_USER_SELECT);
            //SELETE id,uname,upass,type from `user` WHERE uname=? AND upass=?
            // 设置SQL语句中的参数
            preparedStatement.setString(1,uname);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                uname = resultSet.getString("uname");
                String upass = resultSet.getString("upass");
                int type = resultSet.getInt("type");
                return new User(id, uname, upass, type);
            }
        } catch (SQLException throwables) {
            // TODO Auto-generated catch block
            throwables.printStackTrace()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ;
        }finally {
            JDBCUtils.release(connection,preparedStatement,null);
        }

        return null;
    }
}
